package com.steven.love.service;

import com.alibaba.fastjson.JSONObject;
import com.steven.love.dao.DiaryDao;
import com.steven.love.pojo.Diary;
import com.steven.love.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DiaryServiceImp implements DiaryService {

    @Autowired
    private DiaryDao dao;

    @Override
    public JSONObject queryAllLoveInfo(Long page) {
        JSONObject jsonObject = new JSONObject();
        List<Diary> list;
        if (page == null) {
            list = dao.queryAllLoveInfo();
        } else {
            if (page < 1) {
                jsonObject.put("msg", "页码错误");
                return ResultUtils.makeResult(0, jsonObject);
            }
            list = dao.queryLoveInfo(0L, (page - 1) * 20 + 19);
        }
        jsonObject.put("info", list);
        jsonObject.put("msg", "success");
        return ResultUtils.makeResult(0, jsonObject);
    }

    @Override
    public JSONObject addLoveDiary(Diary diary) {
        JSONObject jsonObject = new JSONObject();
        int result = dao.addLoveDiary(diary.getAuthor(), diary.getContent(), System.currentTimeMillis());
        if (result > 0) {
            jsonObject.put("msg", "success");
        } else {
            jsonObject.put("msg", "添加失败");
        }
        return ResultUtils.makeResult(0, jsonObject);
    }

    @Override
    public JSONObject deleteLoveDiary(Long id) {
        JSONObject jsonObject = new JSONObject();
        int result = dao.deleteLoveDiary(id);
        if (result > 0) {
            jsonObject.put("msg", "success");
        } else {
            jsonObject.put("msg", "删除失败");
        }
        return ResultUtils.makeResult(0, jsonObject);
    }

    @Override
    public JSONObject queryAllUnHappyInfo(Long page) {
        JSONObject jsonObject = new JSONObject();
        List<Diary> list;
        if (page == null) {
            list = dao.queryAllUnHappyInfo();
        } else {
            if (page < 1) {
                jsonObject.put("msg", "页码错误");
                return ResultUtils.makeResult(0, jsonObject);
            }
            list = dao.queryUnHappyInfo(0L, (page - 1) * 20 + 19);
        }
        jsonObject.put("info", list);
        jsonObject.put("msg", "success");
        return ResultUtils.makeResult(0, jsonObject);
    }

    @Override
    public JSONObject addUnHappyDiary(Diary diary) {
        JSONObject jsonObject = new JSONObject();
        int result = dao.addUnHappyDiary(diary.getAuthor(), diary.getContent(), System.currentTimeMillis());
        if (result > 0) {
            jsonObject.put("msg", "success");
        } else {
            jsonObject.put("msg", "添加失败");
        }
        return ResultUtils.makeResult(0, jsonObject);
    }

    @Override
    public JSONObject deleteUnHappyDiary(Long id) {
        JSONObject jsonObject = new JSONObject();
        int result = dao.deleteUnhappyDiary(id);
        if (result > 0) {
            jsonObject.put("msg", "success");
        } else {
            jsonObject.put("msg", "删除失败");
        }
        return ResultUtils.makeResult(0, jsonObject);
    }
}
